﻿#ifndef SVGOBALSETTING_H
#define SVGOBALSETTING_H

#include <QDebug>
#include <QImage>
#include <opencv2/core/cuda.hpp>
#include <opencv2/cudaarithm.hpp>
#include <opencv2/cudaimgproc.hpp>
#include <opencv2/opencv.hpp>

#define PCB_TEMPLATE_FEATURES_ORB 1000
#define SV_PERSPECTIVE_SCALE 0.25f
#define MAT_MAX_SIZE 5120
#define MEMPOOL_MAX_COUNT 10

struct PCBTemplateFeatures
{
    cv::Mat mat;
    cv::Mat descriptors;
    std::vector<cv::KeyPoint> keypoints;
};

inline cv::Mat convertQImageToCVImage(const QImage &image)
{
    cv::Mat mat;
    switch (image.format()) {
    case QImage::Format_RGB888:
        mat = cv::Mat(image.height(),
                      image.width(),
                      CV_8UC3,
                      const_cast<uchar *>(image.bits()),
                      image.bytesPerLine());
        cv::cvtColor(mat, mat, cv::COLOR_RGB2BGR);
        break;
    case QImage::Format_Grayscale8:
        mat = cv::Mat(image.height(),
                      image.width(),
                      CV_8UC1,
                      const_cast<uchar *>(image.bits()),
                      image.bytesPerLine());
        break;
    default:
        mat = convertQImageToCVImage(image.convertToFormat(QImage::Format_RGB888));
        break;
    }
    return mat.clone();
}

inline QImage convertCVImageToQImage(const cv::Mat &mat)
{
    switch (mat.type()) {
    case CV_8UC1:
        return QImage(mat.data, mat.cols, mat.rows, mat.step, QImage::Format_Grayscale8).copy();
    case CV_8UC3: {
        cv::Mat temp;
        cv::cvtColor(mat, temp, cv::COLOR_BGR2RGB);
        return QImage(temp.data, temp.cols, temp.rows, temp.step, QImage::Format_RGB888).copy();
    }
    case CV_8UC4:
        return QImage(mat.data, mat.cols, mat.rows, mat.step, QImage::Format_ARGB32).copy();
    default:
        return QImage();
    }
}

#endif
